package com.an.house.service;

import com.an.house.domain.House;
import com.an.house.utils.Utility;

public class HouseService {
  private House[] house;//保存house对象
  private int housenums=0;//记录当前house数量
  private int idcounter=0;//记录id

  //修改房屋


  //查找房屋
  public House search(int serid) {
    for (int i = 0; i < housenums; i++) {
      if (serid == house[i].getId()) {
        return house[i];
      }
    }
    return null;
  }

  //删除房屋
public boolean del(int delid){
  int index=-1;
  //查找要删除房屋下标
  for(int i=0;i<house.length;i++){
    if(i==delid){
      index=i;
      break;
    }
  }
  //房屋找不到
  if(index==-1){
    System.out.println("=======找不到该房屋! 删除失败======");
    return false;
  }
  //正式删除房屋
  for(int i=index;i<housenums;i++){
    house[i]=house[i+1];
  }
  house[--housenums]=null;//把当前房屋最后一个置空
  System.out.println("=========删除成功==========");
  return true;

}
  //添加对象
  public boolean add(House newhouse){
    //判断空间是否满了
    if(housenums==house.length){
      //空间已满 不能添加 需要扩容
      return false;
    }
    //把newhouse添加  新增房屋
     house[housenums++]=newhouse;
    //id自增
   newhouse.setId(++idcounter);
   return true;
  }
  public HouseService(int size){
    house=new House[size];
   // house[0]=new House(1,"javk",10086,"China",2000,"已出租");
  }

  //list 方法 返回house
  public House[] list(){
    return house;
  }
}
